import * as zrender from "zrender";

import { unitWidth, halfUnitWidth, setCurrentGroup } from '../../config'

export function getLeftHandleBar(
  w: number,
  box: any,
  taskBarRect: any,
  redrawChart: RedrawChartFunction,
  tasks: Task[],
  taskIndex: number
) {
  const { height: barHeight, width } = taskBarRect;
  const leftBar = new zrender.Rect({
    name: "leftBar",
    style: {
      fill: `rgba(0, 255, 0, 0)`,
      lineWidth: 1
    },
    shape: {
      width: w,
      height: barHeight
    },
    draggable: "horizontal",
    cursor: "ew-resize",
    z: 1
  });
  leftBar.attr({
    x: box.x,
    y: box.y,
    shape: {
      x: -w / 2,
      y: 0
    }
  });

  let dragStartX = 0;
  let oldX = 0;
  let oldWidth = 0;
  let parentGroup: any = null;
  let taskBarRef: any = null;

  leftBar.on("dragstart", function (e) {
    parentGroup = leftBar.parent;
    if (parentGroup && (parentGroup as any).updateResizingState) {
      (parentGroup as any).updateResizingState(true);
    }
    dragStartX = e.event.zrX;
    taskBarRef = (leftBar as any).taskBar;
    if (taskBarRef) {
      oldWidth = taskBarRef.shape.width;
      oldX = taskBarRef.shape.x;
    }
    setCurrentGroup(parentGroup);
  });

  leftBar.on("drag", function (e) {
    const deltaX = e.event.zrX - dragStartX;
    if (taskBarRef) {
      taskBarRef.attr({
        shape: {
          x: oldX + deltaX,
          width: oldWidth - deltaX
        }
      });
    }
  });

  leftBar.on("dragend", function (e) {
    const deltaX = e.event.zrX - dragStartX;
    const dir = deltaX < 0 ? -1 : 1;
    const delta = Math.abs(deltaX);
    const mod = delta % unitWidth;
    const offsetX = dir * (Math.floor(delta / unitWidth) + Math.floor(mod / halfUnitWidth));
    const task = tasks[taskIndex];
    if (task) {
      task.start += offsetX;
      task.duration -= offsetX;
    }
    // 通知父组调整大小结束
    if (parentGroup && (parentGroup as any).updateResizingState) {
      (parentGroup as any).updateResizingState(false);
    }
    setCurrentGroup(null);
    // Redraw the chart after dragging
    redrawChart(true);
  });
  return leftBar;
}

export function getRightHandleBar(
  w: number,
  box: any,
  taskBarRect: any,
  redrawChart: RedrawChartFunction,
  tasks: Task[],
  taskIndex: number
) {
  const { height: barHeight, width } = taskBarRect;
  const rightBar = new zrender.Rect({
    style: {
      fill: `rgba(255, 192, 203, 0)`,
      lineWidth: 1
    },
    shape: {
      width: w,
      height: barHeight
    },
    draggable: "horizontal",
    cursor: "ew-resize",
    z: 1
  });

  rightBar.attr({
    x: box.x + width,
    y: box.y,
    shape: {
      x: -w / 2,
      y: 0
    }
  });

  let dragStartX = 0;
  let oldWidth = 0;
  let parentGroup: any = null;
  let taskBarRef: any = null;

  rightBar.on("dragstart", function (e) {
    parentGroup = rightBar.parent;
    if (parentGroup && (parentGroup as any).updateResizingState) {
      (parentGroup as any).updateResizingState(true);
    }
    dragStartX = e.event.zrX;
    taskBarRef = (rightBar as any).taskBar;
    if (taskBarRef) {
      oldWidth = taskBarRef.shape.width;
    }
    setCurrentGroup(parentGroup);
  });

  rightBar.on("drag", function (e) {
    const deltaX = e.event.zrX - dragStartX;
    if (taskBarRef) {
      taskBarRef.attr({
        shape: {
          width: oldWidth + deltaX
        }
      });
    }
  });

  rightBar.on("dragend", function (e) {
    const deltaX = e.event.zrX - dragStartX;
    const dir = deltaX > 0 ? 1 : -1;
    const delta = Math.abs(deltaX);
    const mod = delta % unitWidth;
    const offsetX = dir * (Math.floor(delta / unitWidth) + Math.floor(mod / halfUnitWidth));
    const task = tasks[taskIndex];
    if (task) {
      task.duration += offsetX;
    }
    // 通知父组调整大小结束
    if (parentGroup && (parentGroup as any).updateResizingState) {
      (parentGroup as any).updateResizingState(false);
    }
    setCurrentGroup(null);
    // Redraw the chart after dragging
    redrawChart(true);
  });
  return rightBar;
}